home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / comm1 / npns.lha / npns / src / postnews.c < prev    next >
C/C++ Source or Header  |  1996-05-06  |  8KB  |  377 lines

  1.  
  2. /*
  3.  *    Program     postnews
  4.  *    Programmer    N.d'Alterio
  5.  *    Date        06/07/95
  6.  *
  7.  *  Synopsis:    This program spools an news article ready for posting at
  8.  *              some later time. If currently online then sendnews is 
  9.  *              called and the article will be sent off immediately.
  10.  *        The online by checking env var ONLINE
  11.  *
  12.  *        This is a heavily modified version of postnews by 
  13.  *              James Burton from his postnewsspool package. This fixes
  14.  *              a number of bugs such as the leaving of locks, hanging
  15.  *              for a long time while waiting to see if it is online.
  16.  *        In addition the program has been very much streamlined
  17.  *              there are no longer the command line options -X, -R as
  18.  *              these seemd to have no use. Also gone are the arg### and
  19.  *              ref### files it produced which seemed to be useless.
  20.  *    
  21.  *        For tin users the program has the command line option
  22.  *              -f the add a From: user@somewhere to the header.
  23.  *
  24.  *        This program has the paths for my setup compiled into
  25.  *              it as defaults to use different paths and files there
  26.  *              is an env variable PNS_SPOOLDIR which is the full directory
  27.  *              name for the spool dir with no trailing '/'.
  28.  *
  29.  *              The program expects to have the companion program sendnews
  30.  *              in AmiTCP:bin/sendnews which in turn needs to have the 
  31.  *              NNTP posting program as AmiTCP:bin/NNTPPost.     
  32.  *
  33.  *  Command Line Arguments:    -n        Does not send if online
  34.  *                -R        Specifies header filename
  35.  *                -x        File to delete if successful
  36.  *                -f        Adds a From: someone@ line to header
  37.  *                              -h              Prints some help info
  38.  *                filename    The article file to post
  39.  *                                              if omitted will take from STDIN
  40.  *
  41.  *  Inputs:        article            Should contain a full header
  42.  *            or
  43.  *            header            article header
  44.  *            body            article body
  45.  *
  46.  *  Outputs:        spooldir/msg.###     The spooled message
  47.  *
  48.  *  Variables:        int i            general loop var
  49.  *            int exit_code        return code for prog
  50.  *            BPTR lock        lock on spooldir
  51.  *            char *Buffer         string to build filenames
  52.  *            FILE *msg_fptr        file to contain spooled  article
  53.  *            FILE *body_fptr        file containing article body
  54.  *            FILE *head_fptr        fiel containing article header
  55.  *            int no_post        flag - don't try to send
  56.  *            char *from        Pointer to from string
  57.  *            char *body_fname    Pointer to body filename 
  58.  *            char *head_fname    Pointer to header filename
  59.  *            int status        delete status flag
  60.  *            int num_del         Number of files to be deleted
  61.  *            char *Todelete[]    Buffer containing names of files to delete
  62.  *
  63.  *  Functions:        CopyFile        copies article to spooldir
  64.  *            NextArg            checks for existance of next argument
  65.  *
  66.  *  $Id: postnews.c 5.1 1996/05/06 22:59:18 nagd Exp $
  67.  *
  68.  */
  69.  
  70.  
  71. #include "NPNS.h"
  72.  
  73. #define MAX_DEL        16
  74. #define BUFSIZE        256
  75.  
  76. extern char *version = "$VER: postnews" VERSION;
  77.  
  78. int main( int argc,char **argv )
  79.  
  80. {
  81.  
  82.   FILE *msg_fptr;
  83.   FILE *head_fptr;
  84.   FILE *body_fptr;
  85.     
  86.   int i;          
  87.   int exit_code = 0;
  88.   int no_post   = FALSE;
  89.   int num_del;
  90.   int con_status;
  91.   int status;
  92.  
  93.   char *spooldir;
  94.   char *from;
  95.   char *body_fname;
  96.   char *head_fname;
  97.   char *online;
  98.  
  99.   char Buffer[BUFSIZE];
  100.   
  101.   char *Todelete[MAX_DEL];   
  102.  
  103.   BPTR lock;                     
  104.  
  105. /*
  106.  *   Read spool dir from env variable.
  107.  */
  108.  
  109.   if ( ( spooldir = getenv( "PNS_SPOOLDIR" ) ) == NULL ) {
  110.     spooldir = SPOOLDIR;
  111.   }   /* end if */
  112.  
  113. /*
  114.  *   See if user wants to post or not if online.
  115.  */
  116.  
  117.   if ( getenv( "NNTPALWAYSQUEUE" ) ) {
  118.     no_post = TRUE;
  119.   }   /* end if */
  120.  
  121.  
  122. /*
  123.  *=================================================================================================*/
  124.  
  125. /*
  126.  *   Parse command line.
  127.  */
  128.   
  129.   i          = 1;
  130.   num_del    = 0;
  131.   from       = NULL;
  132.   head_fname = NULL;
  133.   body_fname = NULL;
  134.     
  135.   while ( i < argc ) {    
  136.  
  137.     if ( argv[i][0] == '-' ) {   
  138.  
  139.     switch ( argv[i][1] ) {
  140.  
  141. /*
  142.  *  Add a From: line to article.
  143.  */
  144.  
  145.                 case 'f' : 
  146.  
  147.             if ( NextArg( i, argc, argv ) ) {
  148.  
  149.                 from = argv[i+1];
  150.                                 
  151.             } else {
  152.  
  153.                 exit(10);
  154.  
  155.             }  /* end if */
  156.             i = i + 2;
  157.             break;
  158.  
  159. /*
  160.  *   Dont attempt to post if online.
  161.  */
  162.  
  163.         case 'Q' :
  164.  
  165.             no_post = TRUE;
  166.             i++;
  167.             break;
  168.  
  169. /*
  170.  *   Specifies a header for the article.
  171.  */
  172.  
  173.         case 'R' :
  174.  
  175.             if ( NextArg( i, argc, argv ) ) {
  176.                 head_fname = argv[i+1];                        
  177.             } else {
  178.                 exit(10);
  179.             }  /* end if */
  180.             i = i + 2;
  181.             break;
  182.  
  183. /*
  184.  *   Set up a buffer of filenames to delete if 
  185.  *   successful in posting.
  186.  */
  187.                             
  188.         case 'x' :
  189.  
  190.             if ( NextArg( i, argc, argv ) ) {
  191.                 if ( num_del < MAX_DEL ) {
  192.                     Todelete[num_del] = argv[i+1];
  193.                     num_del++;
  194.                 } else {
  195.                     fprintf( stderr, " Option '-x' used to many times ignoring\n" );
  196.                 }   /* end if */
  197.             } else {
  198.                 exit(10);
  199.             }   /* end if */
  200.             i = i + 2;
  201.             break;
  202.  
  203. /*
  204.  *   Print help.
  205.  */
  206.  
  207.         case 'h' :
  208.  
  209.             fprintf( stderr, " Usage: %s article [options] or %s [options] < article\n", argv[0], argv[0] );
  210.                        fprintf( stderr, " Where options are :-\n" );
  211.             fprintf( stderr, "\t-f address\tAdd a From: address line to article\n" );
  212.             fprintf( stderr, "\t-Q        \tDon't try to send article if online\n" );
  213.             fprintf( stderr, "\t-R header \tPut header at top of message\n" );
  214.             fprintf( stderr, "\t-x file   \tFile to delete if posting successful\n" );
  215.             fprintf( stderr, "\t-h        \tFor this help\n\n" );
  216.             exit(0);;
  217.                        break;
  218.  
  219.         default :
  220.  
  221.             fprintf( stderr, " Unknown option use -h for help\n" );
  222.             exit(5);
  223.             break;
  224.  
  225.         }   /* end switch */
  226.  
  227.  
  228.         } else {   
  229.  
  230.         body_fname = argv[i];
  231.         i++;
  232.  
  233.     }   /* end if */
  234.  
  235.   }   /* end while */                    
  236.  
  237. /*
  238.  *=================================================================================================*
  239.  */
  240.  
  241.  
  242. /*
  243.  *   Lock directory while using.
  244.  */
  245.       
  246.   if ( lock = Lock( spooldir, ACCESS_READ ) ) {
  247.  
  248.  
  249. /*
  250.  *   Open file to spool articles to.
  251.  */
  252.  
  253.       sprintf( Buffer, "%s/msg.%ld", spooldir, time( NULL ) );
  254.       if ( msg_fptr = fopen( Buffer, "w" ) ) {
  255.  
  256. /*
  257.  *   Add a from line if requested.
  258.  */
  259.  
  260.         if ( from != NULL ) fprintf( msg_fptr, "From: %s\n", from );
  261.  
  262. /*
  263.  *  Open header file and copy to destination if user
  264.  *  has used -R option.
  265.  */
  266.  
  267.         if ( head_fname != NULL ) {
  268.  
  269.             if ( head_fptr = fopen( head_fname, "r" ) ) {
  270.  
  271.                 CopyFile( head_fptr, msg_fptr );
  272.                 fclose( head_fptr );
  273.  
  274.             } else {
  275.  
  276.                 fprintf( stderr, " Could not open header file - %s\n", head_fname );
  277.                 exit_code = 10;
  278.     
  279.             }   /* end if */
  280.  
  281.         }   /* end if */
  282.  
  283.         if ( !exit_code ) {
  284.  
  285. /*
  286.  *   Open article/body file or set to stdin if no name
  287.  *   has been specified.
  288.  */
  289.  
  290.             if ( body_fname != NULL ) {
  291.         
  292.                 if ( ( body_fptr = fopen( body_fname, "r" ) ) == NULL ) {
  293.  
  294.                     fprintf( stderr, " Could not open body/article file - %s\n", body_fname );
  295.                     exit_code = 10;
  296.  
  297.                 }   /* end if */
  298.  
  299.             } else {
  300.  
  301.                 body_fptr = stdin;
  302.  
  303.             }   /* end if */
  304.  
  305. /*
  306.  *   All files opened correctly so copy article to destination
  307.  *   file.
  308.  */
  309.  
  310.             if ( !exit_code ) {
  311.  
  312.                 CopyFile( body_fptr, msg_fptr );
  313.                 if ( body_fname != NULL ) fclose( body_fptr );
  314.  
  315.  
  316. /*
  317.  *   Successful posting so delete the specified
  318.  *   files
  319.  */
  320.  
  321.                 for ( i = 0; i < num_del; i++ ) {
  322.  
  323.                     status = remove( Todelete[i] );
  324.                     if ( status ) fprintf( stderr, " Failed to delete file - %s\n", Todelete[i] );
  325.  
  326.                 }   /* end for */
  327.  
  328.             }   /* end if */
  329.  
  330.         }   /* end if */
  331.                   
  332.         fclose( msg_fptr );
  333.         if ( exit_code ) remove( Buffer );
  334.  
  335.     } else {
  336.  
  337.         fprintf( stderr, " Could not open message file - %s\n", Buffer );
  338.         exit_code = 10;
  339.  
  340.     }   /* end if open msg file */
  341.  
  342.     UnLock( lock );
  343.  
  344.   } else {
  345.  
  346.     fprintf( stderr, " Could not lock spooldir - %s\n", spooldir );
  347.     exit_code = 20;
  348.  
  349.   }   /* end if lock */
  350.  
  351.     
  352. /*
  353.  *   Check if online. If yes then call sendnews to send off articles
  354.  *   now.
  355.  */
  356.  
  357.   if ( !exit_code && !no_post ) {
  358.  
  359.     if ( ( online = getenv( "ONLINE" ) ) == NULL ) {
  360.         fprintf( stderr, " Please set ONLINE env variable\n" );
  361.         fprintf( stderr, " You are not online - message spooled\n" );
  362.       } else {
  363.         if ( !(con_status = atoi( online )) ) {
  364.             fprintf( stderr, " You are not online - message spooled\n" );
  365.         } else {
  366.             fprintf( stderr, " Posting spooled news now....\n" );
  367.             system("sendnews");
  368.         }  /* end if */
  369.       }   /* end if */
  370.  
  371.   }   /* end if !exit_code */
  372.  
  373.   exit( exit_code );
  374.  
  375. }   /* end program postnews */
  376.  
  377.